package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_063;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *@Description: 福利人员信息推送SAP
 *@Author: Dusongsong
 *@Date: 2019/12/24
 */
public class WeaHRflrytsAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用: WeaHRflrytsAction");
        //String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String workflowid = requestInfo.getWorkflowid();
        rs.executeQuery("select b.tablename from workflow_base a,workflow_bill b where  b.id=a.formid  and a.id =?", workflowid);
        rs.next();
        String tablename = Util.null2String(rs.getString("tablename"));//表名
        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        baseBean.writeLog("select * from " + tablename + " where requestid=" + requestid);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));//外键
        String STATE = "2";
        ZFM_HR_063 hr_063 = new ZFM_HR_063();
        List<Map<String, String>> list = new ArrayList<>();
        String SQL = "select * from " + tablename + "_dt1 where mainid='"+mainid+"'";
        rs.execute(SQL);
        try {
            while (rs.next()) {
                String GJAHR = Util.null2String(rs.getString("GJAHR"));//年份
                String JQFL = Util.null2String(rs.getString("JQLX"));//假期类型
                String PERNR = Util.null2String(rs.getString("PERNR"));//人员编码
                String FLFW = Util.null2String(rs.getString("FLFW"));//部门规定福利范围
                String FLFW1 = Util.null2String(rs.getString("FLFW1"));//部门上报福利范围
                String FLKH = Util.null2String(rs.getString("FLKH"));//福利卡号
                Map<String, String> map = new HashMap();
                map.put("GJAHR", GJAHR);
                map.put("JQFL", JQFL);
                map.put("PERNR", PERNR);
                map.put("FLFW", FLFW);
                map.put("FLFW1", FLFW1);
                map.put("FLKH", FLKH);
                list.add(map);
                baseBean.writeLog("相关参数:" + map.toString());
            }
                Map resultMap = hr_063.execute(list);
                String flag = (String) resultMap.get("flag");
                String message = (String) resultMap.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag + ";message:" + message);
                if ("s".equalsIgnoreCase(flag)) {
                    rs.execute(SQL);
                    while (rs.next()) {
                        String NUMID = Util.null2String(rs.getString("NUMID"));//唯一标识
                        String FLFW1 = Util.null2String(rs.getString("FLFW1"));//部门上报福利范围
                        String FLKH = Util.null2String(rs.getString("FLKH"));//福利卡号
                        String updateSQL = "update uf_sxflrymdxf set  flfw1='" + FLFW1 + "' ,flkh1= '" + FLKH + "', zt='" + STATE + "' where numid='" + NUMID + "'";
                        rs.execute(updateSQL);
                        baseBean.writeLog(updateSQL);

                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } catch(Exception e){
                e.printStackTrace();
                baseBean.writeLog("异常参考:" + e);
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                return Action.FAILURE_AND_CONTINUE;
            }
        return Action.SUCCESS;
    }
}

